home *** CD-ROM | disk | FTP | other *** search
/ The Games Machine 131 / XENIATGM131.iso / Shareware / openOffice.org 641 / Windows / f_0250 / FilesModul.xba < prev    next >
Extensible Markup Language  |  2001-11-15  |  14KB  |  374 lines

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
  3. <script:module xmlns:script="http://openoffice.org/2000/script" script:name="FilesModul" script:language="StarBasic">Option Explicit
  4.  
  5. Public AbsTemplateFound as Integer
  6. Public AbsDocuFound as Integer
  7. Public oLogDocument as Object
  8. Public oLogTable as Object
  9. Public bInsertRow as Boolean
  10.  
  11. Function ReadApplicationDirectories(ApplIndex as Integer, FilesList(),bIsDocument as Boolean, sFiltername()) as Integer
  12. Dim bCheckDocuType as Boolean
  13. Dim FilterIndex as Integer
  14. Dim bRecursive as Boolean
  15. Dim sSourceDir as String
  16. Dim bCheckRealType as Boolean
  17. Dim a as Integer
  18. Dim sFileContent() as String
  19. Dim NewList() as String
  20. Dim Index as Integer
  21. Dim sLocExtension as String
  22.     Index = Applications(ApplIndex,SBAPPLKEY)
  23.     sLocExtension = ""
  24.     If bIsDocument Then
  25.         bCheckDocuType = Applications(ApplIndex,SBDOCCONVERT)
  26.         bCheckRealType = False
  27.         bRecursive = Applications(ApplIndex,SBDOCRECURSIVE)
  28.         FilterIndex = Index
  29.         sSourceDir = Applications(ApplIndex,SBDOCSOURCE)
  30.     Else
  31.         ' Templates
  32.         bCheckDocuType = Applications(ApplIndex,SBTEMPLCONVERT)
  33.         ' In SO the documenttype cannot be derived from the extension name
  34.         bCheckRealType = WizardMode = SBXMLMODE
  35.         If bCheckRealType Then
  36.             ' Note: StarOffice-Math-Documents cannot be treated like templates
  37.             bCheckRealType = Index <> 3
  38.             If bCheckRealType Then
  39.                 sLocExtension = "vor"
  40.             End If
  41.             bIsDocument = Not bCheckRealType
  42.         End If
  43.         bRecursive = Applications(ApplIndex,SBTEMPLRECURSIVE)
  44.         FilterIndex = Index + MaxApplCount
  45.         sSourceDir = Applications(ApplIndex,SBTEMPLSOURCE)
  46.     End If
  47.     If bCheckDocuType Then
  48.         sFileContent() = GetMimeTypeList(sFilterName(FilterIndex))
  49.         NewList() = ReadDirectories(sSourceDir, bRecursive, bCheckRealType, False, sFileContent(), sLocExtension)
  50.         If Ubound(NewList()) > -1 Then
  51.             AddListtoFilesList(FilesList(), NewList(), ApplIndex)
  52.             ImportDialog.LabelRetrieval.Label = sProgressPage_2 &  "  " & ReplaceString(sProgressPage_5, Str(Ubound(FilesList()) + 1) & " ", "%1")
  53.         End If
  54.     End If
  55.     ReadApplicationDirectories() = Ubound(NewList(),1) + 1
  56. End Function
  57.  
  58.  
  59. Sub    ShowCurrentProgress(bIsDocument as Boolean, CurFound as Integer)
  60.     If bIsDocument Then
  61.         AbsDocuFound = AbsDocuFound + CurFound
  62.         ImportDialog.LabelCurDocumentRetrieval.Label =  sProgressFound & " " & CStr(AbsDocuFound) &  " " & sProgressMoreDocs
  63.     Else
  64.         AbsTemplateFound = AbsTemplateFound + CurFound
  65.         ImportDialog.LabelCurTemplateRetrieval.Label = sProgressFound & " " & CStr(AbsTemplateFound) & " " & sProgressMoreTemplates
  66.   End If
  67. End Sub
  68.  
  69.  
  70. Sub ConvertAllDocuments(sFilterName())
  71. Dim FileProperties(0) as new com.sun.star.beans.PropertyValue
  72. Dim WriterWebProperties(0) as new com.sun.star.beans.PropertyValue
  73. Dim OpenProperties(1) as new com.sun.star.beans.PropertyValue
  74. Dim FilesList(0,2) as String
  75. Dim sViewPath as String
  76. Dim i as Integer
  77. Dim FilterIndex as Integer
  78. Dim sFullName as String
  79. Dim sFileName as String
  80. Dim oDocument as Object
  81. Dim sExtension as String
  82. Dim OldExtension as String
  83. Dim CurFound as Integer
  84. Dim TotFound as Integer
  85. Dim TargetStemDir as String
  86. Dim SourceStemDir as String
  87. Dim TargetDir as String
  88. Dim TargetFile as String
  89. Dim CurFilterName as String
  90. Dim ApplIndex as Integer
  91. Dim Index as Integer
  92. Dim bIsDocument as Boolean
  93. Dim iOverWrite as Integer
  94. Dim bDoSave as Boolean
  95. Dim sCurFileExists as String
  96. Dim oTaskEnum as Object
  97. Dim oTask as Object
  98. Dim oModel as Object
  99. Dim oTaskController as Object
  100. Dim MaxFileIndex as Integer
  101. Dim sOldExtension as String
  102.     bConversionIsRunnig = True
  103.     AbsTemplateFound = 0
  104.     AbsDocuFound = 0
  105.     For i = 0 To ApplCount-1
  106.         'templates
  107.         bIsDocument = False
  108.         CurFound = ReadApplicationDirectories(i, FilesList(), bIsDocument, sFilterName())
  109.         ShowCurrentProgress(bIsDocument, CurFound)
  110.     Next i
  111.     For i = 0 To ApplCount-1
  112.         'documents
  113.         bIsDocument = True
  114.         CurFound = ReadApplicationDirectories(i, FilesList(), bIsDocument, sFilterName())
  115.         ShowCurrentProgress(bIsDocument, CurFound)
  116.     Next i
  117.     TotFound = AbsTemplateFound + AbsDocuFound
  118.     CreateLogDocument(OpenProperties())
  119.     If TotFound > 0 Then
  120.         InitializeProgressPage(ImportDialog)
  121.         OpenProperties(0).Name = "Hidden"
  122.         OpenProperties(0).Value = True
  123.         OpenProperties(1).Name = "AsTemplate"
  124.         MaxFileIndex = Ubound(FilesList(),1)
  125.         For i = 0 To MaxFileIndex
  126.             If bCancelTask Then
  127.                 bConversionIsRunnig = False
  128.                 Exit Sub
  129.             End if
  130.             bDoSave = True
  131.             sFullName = FilesList(i,0)
  132.             CurFiltername =    GetFilterName(FilesList(i,1), sFilterName(), sExtension, FilterIndex)
  133.             ApplIndex = FilesList(i,2)
  134.             sViewPath = CutPathView(sFullName, 60)
  135.             ImportDialog.LabelCurDocument.Label = Str(i+1) & "/" & MaxFileIndex + 1 & "  (" & sViewPath & ")"
  136.             
  137.             sOldExtension = GetFileNameExtension(sFullName, "/")
  138.             Select Case sOldExtension
  139. ' Todo: This code should be simplified as soon as it is clear how 'LoadComponentfromUrl' exactly works with 
  140. ' templates and documents
  141.                 Case "vor", "dot", "xlt", "pot"
  142.                     OpenProperties(1).Value = False
  143.                 Case Else
  144.                     OpenProperties(1).Value = False
  145.             End Select
  146.             oDocument = StarDesktop.LoadComponentFromURL(sFullName, "_blank", 0, OpenProperties())
  147.             If bSetFonts Then
  148.                 CheckScripts(oDocument, 1)
  149.             End If
  150.  
  151.             If Not IsNull(oDocument) Then
  152.                 Select Case sExtension
  153.                     Case "sxw", "sxc", "sxi", "sxd", "sxs", "sxm"
  154.                         SourceStemDir = RTrimStr(Applications(ApplIndex,SBDOCSOURCE), "/")
  155.                         TargetStemDir = RTrimStr(Applications(ApplIndex,SBDOCTARGET), "/")
  156.                     Case Else                                 ' Templates and Helper-Applications remain
  157.                         SourceStemDir = RTrimStr(Applications(ApplIndex,SBTEMPLSOURCE), "/")
  158.                         TargetStemDir = RTrimStr(Applications(ApplIndex,SBTEMPLTARGET), "/")
  159.                 End Select
  160.  
  161.                 TargetFile = ReplaceString(sFullname, TargetStemDir, SourceStemDir)
  162.                 sFileName = GetFileNameWithoutExtension(TargetFile, "/")
  163.                 OldExtension = GetFileNameExtension(TargetFile)
  164.  
  165.                 TargetFile = RTrimStr(TargetFile, OldExtension)
  166.                 TargetFile = TargetFile & sExtension
  167.                 TargetDir = RTrimStr(TargetFile, sFileName & "." & sExtension)
  168.                 If Not oUcb.Exists(TargetDir) Then
  169.                     oUcb.CreateFolder(TargetDir)
  170.                 End If
  171.                 If oUcb.Exists(TargetFile) Then
  172.                     sCurFileExists = ReplaceString(sFileExists, ConvertFromUrl(TargetFile), "<1>")
  173.                     sCurFileExists = ReplaceString(sCurFileExists, chr(13), "<CR>")
  174.                     iOverWrite = Msgbox (sCurFileExists, 32 + 3, sTitle)
  175.                     Select Case iOverWrite
  176.                         Case 1    ' OK
  177.                             ' In the FileProperty-Bean this is already default
  178.                             bDoSave = True
  179.                         Case 2     ' Abort
  180.                             CancelTask(False)
  181.                             bDoSave = False
  182.                         Case 7     ' No
  183.                             bDoSave = False
  184.                     End Select
  185.                 End If
  186.                 If bDoSave Then
  187.                     InsertDocNamesToLogDocument(sFullName, TargetFile)
  188.                     On Local Error Resume Next
  189.                     FileProperties(0).Name = "FilterName"
  190.                     FileProperties(0).Value = CurFilterName
  191.                     oDocument.StoreAsUrl(TargetFile,FileProperties())
  192.                     oDocument.Dispose()
  193.                     On Local Error Goto 0
  194.                 End If
  195. '                oTaskenum = StarDesktop.Tasks.CreateEnumeration
  196. '                While oTaskEnum.HasmoreElements
  197. '                    oTask = oTaskenum.NextElement
  198. '                    If oTask.Name <> "" Then
  199. '                        oTaskController = oTask.Controller
  200. '                        PrintdbgInfo oTaskController
  201. '                        If hasUnoInterfaces(oTaskController,"com.sun.star.frame.XModel") then
  202. '                            oModel = oTaskController.Model
  203. '                            If Ucase(oModel.Url) = Ucase(sFullName) Then
  204. '                                oTask.Close
  205. '                            End If
  206. '                        End If
  207. '                    End If
  208. '                Wend
  209.             End If
  210.         Next i
  211.     End If
  212.     ImportDialog.cmdCancel.Label = sCloseButton
  213.     ImportDialog.cmdGoOn.Label = sReady
  214.     ImportDialog.cmdGoOn.Enabled = True
  215.     bConversionIsRunnig = False
  216.     Exit Sub
  217. RTError:
  218.     Msgbox sRTErrorDesc, 16, sRTErrorHeader
  219. End Sub
  220.  
  221.  
  222. Sub AddListtoFilesList(FirstList(), SecList(), ApplIndex as Integer)
  223. Dim FirstStart as Integer, FirstEnd as Integer, i as Integer, s as Integer
  224.     If FirstList(0,0) = "" Then
  225.         FirstStart = Ubound(FirstList(),1)
  226.     Else
  227.         FirstStart = Ubound(FirstList(),1) + 1
  228.     End If
  229.     FirstEnd = FirstStart + Ubound(SecList(),1)
  230.     ReDim Preserve FirstList(FirstEnd,2)
  231.     s = 0
  232.     For i = FirstStart To FirstEnd
  233.         FirstList(i,0) = SecList(s,0)
  234.         FirstList(i,1) = SecList(s,1)
  235.         FirstList(i,2) = CStr(ApplIndex)
  236.         s = s + 1
  237.     Next i
  238. End Sub
  239.  
  240.  
  241. Function GetTargetTemplatePath(Index as Integer)
  242.     Select Case WizardMode
  243.         Case SBMICROSOFTMODE
  244.             GetTargetTemplatePath() = SOTemplatePath & "/" & sTemplateGroupName
  245.         Case SBXMLMODE
  246.             If Index = 3 Then
  247.                 ' Helper Application
  248.                 GetTargetTemplatePath = SOWorkPath
  249.             Else
  250.                 GetTargetTemplatePath = SOTemplatePath
  251.             End If
  252.     End Select
  253. End Function
  254.  
  255.  
  256. ' Retrieves the second value for a next to 'SearchString' in
  257. ' a two-dimensional string-Array
  258. Function GetFilterName(sMimetypeorExtension as String, sFilterName(), sExtension as string, FilterIndex as Integer) as String
  259. Dim i as Integer
  260. Dim MaxIndex as Integer
  261. Dim sLocFilterlist() as String
  262.     For i = 0 To Ubound(sFiltername(),1)
  263.         If Instr(1,sFilterName(i,0),sMimeTypeOrExtension) <> 0 Then
  264.             sLocFilterList() = ArrayoutofString(sFiltername(i,0),"|", MaxIndex)
  265.             If MaxIndex = 0 Then
  266.                 sExtension = sFiltername(i,2)
  267.                 GetFilterName = sFilterName(i,1)
  268.             Else
  269.                 Dim a as Integer
  270.                 Dim sLocExtensionList() as String
  271.                 a =    SearchArrayForPartString(sMimetypeOrExtension, sLocFilterList())
  272.                 sLocFilterList() = ArrayoutofString(sFiltername(i,1),"|", MaxIndex)
  273.                 GetFilterName = sLocFilterList(a)
  274.                 sLocExtensionList() = ArrayoutofString(sFilterName(i,2), "|", MaxIndex)
  275.                 sExtension = sLocExtensionList(a)
  276.             End If
  277.             Exit For
  278.         End If
  279.     Next
  280.     FilterIndex = i
  281. End Function
  282.  
  283.  
  284. Function SearchArrayforPartString(SearchString as String, LocList()) as Integer
  285. Dim i as integer
  286.     For i = Lbound(LocList(),1) to Ubound(LocList(),1)
  287.         If Instr(1,LocList(i), SearchString) <> 0 Then
  288.             SearchArrayForPartString() = i
  289.             Exit Function
  290.         End if
  291.     Next
  292.     IndexinArray = -1
  293. End Function
  294.  
  295.  
  296. Function GetMimeTypeList(BigFiltername as STring)
  297. Dim sBigList() as String
  298. Dim sSmallList() as String
  299. Dim sMimeTypeList()
  300. Dim BigMaxIndex as Integer
  301. Dim n as Integer
  302.     sBigList() = ArrayoutofString(BigFilterName,"|", BigMaxIndex)
  303.     For n = 0 To BigMaxIndex
  304.         sSmallList() = ArrayoutofString(sBigList(n),";")
  305.         sMimeTypeList() = AddListToList(sMimeTypeList(), sSmallList())
  306.     Next n
  307.     GetMimetypeList() = sMimeTypeList()
  308. End Function
  309.  
  310.  
  311. Sub CreateLogDocument(HiddenProperties())
  312. Dim oTableCursor as Object
  313. Dim oLogCursor as Object
  314. Dim oLogRows as Object
  315. Dim sLogUrl as String
  316. Dim NoArgs()
  317. Dim i as Integer
  318. Dim bLogExists as Boolean
  319.     If ImportDialog.chkLogfile.State = 1 Then
  320.         i = 2
  321.         oLogDocument = StarDesktop.LoadComponentFromURL("private:factory/swriter", "_blank", 4, NoArgs())' HiddenProperties()) ' HiddenProperties())
  322.         oLogCursor = oLogDocument.Text.CreateTextCursor
  323.         oLogTable =  oLogDocument.CreateInstance("com.sun.star.text.TextTable")
  324.         oLogCursor.Text.InsertTextContent(oLogCursor, oLogTable, True)
  325.         oLogCursor = oLogTable.GetCellbyPosition(0,0).createTextCursor
  326.         oLogCursor.SetString(sSourceDocuments)
  327.         oLogCursor = oLogTable.GetCellbyPosition(1,0).createTextCursor
  328.         oLogCursor.SetString(sTargetDocuments)
  329.         bInsertRow = False
  330.         sLogUrl = SOWorkPath & "/Logfile.sxw"
  331.         Do
  332.             bLogExists = oUcb.Exists(sLogUrl)
  333.             If bLogExists Then
  334.                 If i = 2 Then
  335.                     sLogUrl = ReplaceString(sLogUrl, "/Logfile_2.sxw", "/Logfile.sxw")
  336.                 Else
  337.                     sLogUrl = ReplaceString(sLogUrl, "/Logfile_" & cStr(i) & ".sxw", "/Logfile_" & cStr(i-1) & ".sxw")
  338.                 End If
  339.                 i = i + 1
  340.             End If
  341.         Loop Until Not bLogExists
  342.         oLogDocument.StoreAsUrl(sLogUrl, NoArgs())
  343.     EndIf
  344. End Sub
  345.  
  346.  
  347. Sub InsertDocNamesToLogDocument(SourceUrl as String, TargetUrl as String)
  348. Dim oCell as Object
  349. Dim oLogCursor as Object
  350. Dim UrlList(1) as String
  351. Dim LocFileName as String
  352. Dim LocUrl as String
  353. Dim i as Integer
  354.     If ImportDialog.chkLogfile.State = 1 Then
  355.         If bInsertRow Then
  356.             oLogTable.Rows.InsertByIndex(oLogTable.Rows.Count,1)
  357.         Else
  358.             bInsertRow = True
  359.         End If
  360.         UrlList(0) = SourceUrl
  361.         UrlList(1) = TargetUrl
  362.         For i = 0 To 1
  363.             oCell = oLogTable.GetCellbyPosition(i,oLogTable.Rows.Count-1)
  364.             oLogCursor = oCell.createTextCursor()
  365.             LocUrl = UrlList(i)
  366.             oLogCursor.HyperLinkURL = LocUrl
  367.             oLogCursor.HyperLinkName = LocUrl
  368.             oLogCursor.HyperLinkTarget = LocUrl
  369.             LocFileName = FileNameOutOfPath(LocUrl)
  370.             oCell.InsertString(oLogCursor, LocFileName,False)
  371.         Next i 
  372.         oLogDocument.Store()
  373.     End If
  374. End Sub</script:module>